# Maintainer: Martell Malone <martell malone at g mail dot com >

_realname=libwebsockets
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.0.20
pkgrel=1
arch=('any')
pkgdesc="C library for websocket clients and servers (mingw-w64)"
url='https://libwebsockets.org/'
license=('LGPL2.1')
depends=("${MINGW_PACKAGE_PREFIX}-zlib" "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/warmcat/${_realname}/archive/v${pkgver}.tar.gz"
        001-posix-layout.patch)
options=(!strip !buildflags staticlibs)
sha256sums=('a26d243f2642a9b810e7d91f1e66b149d1da978decdca58ce1c9218c454f397e'
            '9bdfdffc74e348692b20b08881ec682ee7aa7ba65b370199dd52f99331013cdc')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-posix-layout.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DLWS_WITH_DIR=OFF \
    -DLWS_WITHOUT_DAEMONIZE=ON \
    -DLWS_WITHOUT_TESTAPPS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/${_realname}/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
